﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Audit_Configuration_Oracle.Model;
using Audit_Configuration_Oracle.Controllers;

namespace Audit_Configuration_Oracle.Views
{
    public partial class ACO_LogIn : Form
    {
        public bool isLogged = false;

        public ACO_LogIn()
        {
            InitializeComponent();            
        }

        private void btnLogIn_Click(object sender, EventArgs e)
        {
            string user = string.Empty;
            string password = string.Empty;
            string db = string.Empty;
            string msg  = string.Empty;

            user = txtUser.Text;
            password = txtPass.Text;
            //db = cboSchema.SelectedValue.ToString();
            try
            {
                if (user.Equals("") || password.Equals(""))
                {
                    MessageBox.Show("Please indicate your username and password",
                        "Missing data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    isLogged = (LogInController.Connect(user, password, db, out msg ));
                    if (isLogged)
                    {
                        this.Close();
                    }
                    
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("Can not connect to database, please check your user information",
                       "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }

        }

        private void ACO_LogIn_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isLogged)
            {
                Environment.Exit(-1);
            }
            /*if (!isLogged && e.CloseReason.Equals(CloseReason.UserClosing))
            {
                e.Cancel = true;
            }*/
        }
    }
}
